package cn.xopencode.recordservice.service.lowroom;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.xopencode.common.framework.exception.util.ServiceExceptionUtil;
import cn.xopencode.common.framework.util.DateUtil;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.convert.lowroom.LowRoomRecordConvert;
import cn.xopencode.recordservice.convert.lowroom.LowRoomRecordRelConvert;
import cn.xopencode.recordservice.convert.robotroom.AdminRecordConvert;
import cn.xopencode.recordservice.dal.mysql.dataobject.lowroom.LowRoomRecordDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.lowroom.LowRoomRecordEmpRelDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.pdroom.PowerDistributionRoomDO;
import cn.xopencode.recordservice.dal.mysql.mapper.lowroom.LowRoomRecordMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.lowroom.LowRoomRecordRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.pdroom.PowerDistributionRoomMapper;
import cn.xopencode.recordservice.rpc.lowroom.vo.LowRoomRecordAdminPageVO;
import cn.xopencode.recordservice.rpc.lowroom.vo.LowRoomRecordPageVO;
import cn.xopencode.recordservice.rpc.lowroom.vo.LowRoomRecordUpdateVO;
import cn.xopencode.recordservice.service.lowroom.bo.LowRoomRecordCreateBO;
import cn.xopencode.recordservice.service.lowroom.bo.LowRoomRecordPageBO;
import cn.xopencode.recordservice.service.lowroom.bo.LowRoomRecordUpdateBO;
import cn.xopencode.recordservice.service.robotroom.bo.AdminRecordPageBO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.xopencode.recordservice.enums.RecordErrorCodeConstants.RECORD_LOW_ROOM_NOT_EXITS;
import static cn.xopencode.recordservice.enums.RecordErrorCodeConstants.RRECORD_LOW_ROOM_NOT_EXCEPTION;

/**
 * 低压配电室巡检记录 service
 */

@Service
@Validated
public class LowRoomRecordService {

    @Autowired
    private LowRoomRecordMapper lowRoomRecordMapper;
    @Autowired
    private LowRoomRecordRelMapper lowRoomRecordRelMapper;
    @Autowired
    private PowerDistributionRoomMapper powerDistributionRoomMapper;

    public Integer createLowRoomRecord(LowRoomRecordCreateBO createBO, Set<String> empIds){
        LowRoomRecordDO recordDO = LowRoomRecordConvert.INSTANCE.convert(createBO);
        String recordId = IdUtil.objectId();
        recordDO.setId(recordId);
        Integer result = lowRoomRecordMapper.insert(recordDO);
        //保存关联的巡检人姓名信息
        if (!CollectionUtil.isEmpty(empIds)) {
            List<LowRoomRecordEmpRelDO> lowRoomRecordRels = empIds.stream()
                    .map(empId -> new LowRoomRecordEmpRelDO().setId(IdUtil.objectId()).setRecordId(recordId).setEmpId(empId)).collect(Collectors.toList());
            lowRoomRecordRelMapper.insertList(lowRoomRecordRels);
        }
        return result;

    }
    public PageResult<LowRoomRecordPageVO> pageLowRoomRecord(LowRoomRecordPageBO pageBO) {
        IPage<LowRoomRecordDO> recordDOIPage = lowRoomRecordMapper.selectPage(pageBO);
        PageResult<LowRoomRecordPageVO> recordPageVOPageResult =  LowRoomRecordConvert.INSTANCE.convertPage(recordDOIPage);
        if(!CollectionUtil.isEmpty(recordPageVOPageResult.getList())){
            List<LowRoomRecordPageVO> list = recordPageVOPageResult.getList();
            for(LowRoomRecordPageVO roomVO : list){
                List<LowRoomRecordEmpRelDO> relDOList = lowRoomRecordRelMapper.selectListByLowRoomRecordId(roomVO.getId());
                List<String> tmp = relDOList.stream().map(LowRoomRecordEmpRelDO::getEmpId).collect(Collectors.toList());
                roomVO.setEmpIds(String.join(",", tmp));
            }
        }
        return recordPageVOPageResult;
    }

    public LowRoomRecordUpdateVO getLowRoomRecordById(String id){
        LowRoomRecordDO recordDO = lowRoomRecordMapper.selectById(id);
        if(recordDO != null){
            LowRoomRecordUpdateVO recordVo = LowRoomRecordConvert.INSTANCE.convertUpdate(recordDO);
            //查询所有关联的巡检组
            List<LowRoomRecordEmpRelDO> relDOList = lowRoomRecordRelMapper.selectListByLowRoomRecordId(id);
            recordVo.setEmpList(LowRoomRecordRelConvert.INSTANCE.convertList2(relDOList));

            PowerDistributionRoomDO roomDO = powerDistributionRoomMapper.selectById(recordDO.getRoomId());
            if(roomDO != null)
                recordVo.setRoomName(roomDO.getRoomName());
            return recordVo;
        }
        return null;
    }

    public Integer updateLowRoomRecord(LowRoomRecordUpdateBO bo, Set<String> empIds){
        LowRoomRecordDO recordDO = LowRoomRecordConvert.INSTANCE.convert(bo);
        int result = lowRoomRecordMapper.updateById(recordDO);

        //先删除所有关联的班组
        lowRoomRecordRelMapper.deleteByLowroomId(recordDO.getId());
        String recordId = recordDO.getId();
        //保存关联的班组信息
        if (!CollectionUtil.isEmpty(empIds)) {
            List<LowRoomRecordEmpRelDO> lowRoomRecordRels = empIds.stream()
                    .map(empId -> new LowRoomRecordEmpRelDO().setId(IdUtil.objectId()).setRecordId(recordId).setEmpId(empId)).collect(Collectors.toList());
            lowRoomRecordRelMapper.insertList(lowRoomRecordRels);
        }
        return result;
    }


    public Integer dealLowRoomRecordException(String recordId){
        LowRoomRecordDO recordDO = lowRoomRecordMapper.selectById(recordId);
        if(recordDO == null){
            throw ServiceExceptionUtil.exception(RECORD_LOW_ROOM_NOT_EXITS);
        }
        if(recordDO != null && recordDO.getNoticeLeader() != true){
            throw ServiceExceptionUtil.exception(RRECORD_LOW_ROOM_NOT_EXCEPTION);
        }
        recordDO.setLeaderDeal(true);
        return lowRoomRecordMapper.updateById(recordDO);
    }

    /**
     * 后台：根据查询条件分页获取低压配电室巡检记录
     * @param pageBO
     * @return
     */
    public PageResult<LowRoomRecordAdminPageVO> getPageAdminLowRoomRecord(AdminRecordPageBO pageBO){
        Page<LowRoomRecordDO> page = new Page<>(pageBO.getPageNo(),pageBO.getPageSize());
        if (pageBO.getInspectionTime() != null){
            String date = DateUtil.format(pageBO.getInspectionTime(), "yyyy-MM-dd");
            pageBO.setInspectionTime1(date.concat("00:00:00"));
            pageBO.setInspectionTime2(date.concat("12:59:59"));
        }
        IPage<LowRoomRecordDO> recordDOList = lowRoomRecordMapper.adminQuery(page, pageBO);
        return AdminRecordConvert.INSTANCE.convert7(recordDOList);
    }
}
